const app =getApp()
const formatTime = date => {
  if (!date) {
    return '';
  }
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

const formatTime2 = date => {
  if (!date) {
    return '';
  }
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return [year, month, day].map(formatNumber).join('-') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}


function wxuuid() {
  var d = new Date().getTime();
  var uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
    var r = (d + Math.random() * 16) % 16 | 0;
    d = Math.floor(d / 16);
    return (c == 'x' ? r : (r & 0x3 | 0x8)).toString(16);
  });
  return uuid;
}

//获取位置设置信息
const GetSetting = (init,refused)=>{  
  init = typeof (init) === 'function' ? init : function (res) { };
  refused = typeof (refused) === 'function' ? refused : function (res) { };    
  wx.getSetting({      
      success: (res) => {        
          console.log(res);        
          console.log(res.authSetting['scope.userLocation']);        
          if (res.authSetting['scope.userLocation'] != undefined && res.authSetting['scope.userLocation'] != true) {          
              //非初始化进入该页面,且未授权         
              wx.showModal({            
                  title: '是否授权当前位置',            
                  content: '需要获取您的地理位置，请确认授权，否则无法获取地址',            
                  success: function (res) {              
                      if (res.cancel) {                
                          console.info("1授权失败返回数据");
                          refused();                          
                      } else if (res.confirm) {                                          
                          wx.openSetting({                  
                              success: function (data) {                    
                                  console.log(data);                    
                                  if (data.authSetting["scope.userLocation"] == true) {                      
                                      wx.showToast({                        
                                          title: '授权成功',                        
                                          icon: 'success',                        
                                          duration: 2000                      
                                      })           
                                  } else {                      
                                      wx.showToast({                        
                                          title: '授权失败',                        
                                          icon: 'none',                        
                                          duration: 2000                      
                                      });                     
                                  }                  
                              }                
                          })              
                      }            
                  }          
              })        
          } else if (res.authSetting['scope.userLocation'] == true){          
              init();        
          } else if (res.authSetting['scope.userLocation'] == undefined) {          
              //初始化进入          
              init();           
          }     
      }   
  })
}
const GetLocation = (success,fail)=>{  
  success = typeof (success) === 'function' ? success : function (res) { };  
  fail = typeof (fail) === 'function' ? fail : function (res) { };  
  wx.getLocation({    
      type: 'wgs84',    
      success:success,    
      fail:fail  
  })
}

// 获取2022年1月1日
const getBeginTime = () => {
  let d = new Date();
  d.setFullYear(2022);
  d.setMonth(0);
  d.setDate(1);
  return d;
}


const getOneHourLate = () => {
  let d = new Date(new Date().getTime() + 60 * 60 * 1000);
  d.setSeconds(0);
  return d;
}

const getOrderState = (code) => {
  if (code === 0) return "待接单";
  else if (code === 1) return "待回收";
  else if (code === 2) return "已回收";
  else if (code === 3) return "已完成";
  else if (code === 4) return "已取消";
  else if (code === 5) return "已超时";
  else return "";
}

const yuanToFen = (price) => {
  if (!price) return 0;
  let p = parseFloat(price);
  return parseInt(p * 100);
}

const fenToYuan = (price) => {
  if (!price) return 0.0;
  return parseFloat(price)/100.00;
}

const kgTog = (weight) => {
  if (!weight) return 0;
  let w = parseFloat(weight);
  return parseInt(w * 1000);
}

const gToKg = (weight) => {
  if (!weight) return 0.000;
  return parseFloat(weight)/1000.000;
}

// 计算小数点后有几位
const countPoint = (num) => {
  if (!num) return 0;
  const str = String(num);
  return str.length - str.indexOf('.') - 1;
} 


module.exports = {
  formatTime,
  wxuuid,
  formatTime2,
  GetSetting,  
  GetLocation,
  getBeginTime,
  getOneHourLate,
  getOrderState,
  fenToYuan,
  yuanToFen,
  kgTog,
  gToKg,
  countPoint
}
